package cn.zkc.pattern.Singleton;

import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : kay
 * @Date : 2020-07-03 17:29
 **/

public class SingletonTest {

    public static void singleTest1(){
        Singleton a = Singleton.getInstance();
        Singleton b = Singleton.getInstance();
        System.out.println("a == b:"+ (a == b) );
        System.out.println("a.equal(b):" + a.equals(b));
        System.out.println(a.hashCode());
        System.out.println(b.hashCode());
    }

    public static void singleTest2(int num){

        Map map = new HashMap();

        for (int i = 1; i <= num; i++) {
            map.put(i,Singleton.getInstance());
        }

        for (int i = 1; i <= num; i++) {
            System.out.println(map.get(i).hashCode());
        }
    }

    public static void singleTest3(){
        LazySingleton a = LazySingleton.getLazySingleton();
        LazySingleton b = LazySingleton.getLazySingleton();
        System.out.println("a == b:"+ (a == b) );
        System.out.println("a.equal(b):" + a.equals(b));
        System.out.println(a.hashCode());
        System.out.println(b.hashCode());
    }

    public static void singleTest4(int num){
        Map map = new HashMap();
        for (int i = 1; i <= num; i++) {
            map.put(i,LazySingleton.getLazySingleton());
        }

        for (int i = 1; i <= num; i++) {
            System.out.println(map.get(i).hashCode());
        }
    }

    public static void singleTest5(int num){
        Map map = new HashMap();
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < num  ; i++) {
                        Thread.sleep(1);
                        System.out.println("thread1:" + Singleton.getInstance());
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < num  ; i++) {
                        Thread.sleep(2);
                        System.out.println("thread2:" + Singleton.getInstance());
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < num  ; i++) {
                        Thread.sleep(2);
                        System.out.println("thread3:" + Singleton.getInstance());
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        t2.start();
        t3.start();
    }

    public static void main(String[] args) {
//        singleTest1(); //饿汉式测试
//        singleTest2(300);

//        singleTest3();
//        singleTest4(100);
          singleTest5(100);
    }
}
